package com.sunxd.rule.judge.support;

import com.google.common.collect.Maps;
import com.sunxd.rule.judge.ConditionJudge;
import com.sunxd.rule.judge.beans.enums.OperatorTypeEnum;
import com.sunxd.rule.judge.beans.param.Condition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Component
@Slf4j
public class DefaultConditionSupportJudge extends AbstractConditionSupportJudge {

    private final Map<String, ConditionJudge> helperMap;

    @Autowired
    public DefaultConditionSupportJudge(List<ConditionJudge> conditionHelpers) {
        helperMap = Maps.newHashMap();
        if (!CollectionUtils.isEmpty(conditionHelpers)) {
            for (ConditionJudge helper : conditionHelpers) {
                if (helper instanceof DefaultConditionSupportJudge) {
                    continue;
                }
                helperMap.put(helper.getClassType(), helper);
            }
        }
    }

    @Override
    public Boolean gt(Object left, Object right) {
        boolean result;
        ConditionJudge conditionHelper = helperMap.get(right.getClass().getSimpleName());
        if (Objects.nonNull(conditionHelper)) {
            result = conditionHelper.gt(left, right);
        } else {
            result = super.gt(left, right);
        }
        return result;
    }

    @Override
    public Boolean lt(Object left, Object right) {
        boolean result;
        ConditionJudge conditionHelper = helperMap.get(right.getClass().getSimpleName());
        if (Objects.nonNull(conditionHelper)) {
            result = conditionHelper.gt(left, right);
        } else {
            result = super.gt(left, right);
        }
        return result;
    }

    @Override
    public Boolean eq(Object left, Object right) {
        boolean result;
        ConditionJudge conditionHelper = helperMap.get(right.getClass().getSimpleName());
        if (Objects.nonNull(conditionHelper)) {
            result = conditionHelper.eq(left, right);
        } else {
            result = super.eq(left, right);
        }
        return result;
    }

    @Override
    public Boolean in(Object left, Object right) {
        Boolean result;
        ConditionJudge conditionHelper = helperMap.get(right.getClass().getSimpleName());
        if (Objects.nonNull(conditionHelper)) {
            result = conditionHelper.in(right, left);
        } else {
            result = super.in(left, right);
        }
        return result;
    }

    @Override
    public Boolean invoke(Condition condition) {
        ConditionJudge conditionHelper = helperMap.get(OperatorTypeEnum.INVOKE.name());
        if (Objects.nonNull(conditionHelper)) {
            return conditionHelper.invoke(condition);
        }
        return Boolean.FALSE;
    }


    @Override
    public String getClassType() {
        return "Default";
    }

}
